package com.tlx.app.dedao;

import java.io.File;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import com.alibaba.fastjson.JSONObject;
import com.tlx.core.util.TFileUtils;
import com.tlx.core.util.TSimpleThreadPool;
import com.tlx.core.util.TSpring;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.tlx" })
@SuppressWarnings({ "rawtypes", "unused" })
public class DeDaoService {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(DeDaoService.class, args);
        DeDaoService ds = TSpring.getBean(DeDaoService.class);
        System.setProperty("realRename", "true");
        String rootSrc = "D:/0";
        String rootDest = "D:/1";
        String gFile = "D:/0/name.json";
        String json = FileUtils.readFileToString(new File(gFile));
        JSONObject jo = JSONObject.parseObject(json);
        System.out.println("读取group数量：" + jo.size());
        ds.startByDir(rootSrc, rootDest, jo);
        System.out.println("主线程处理完毕");
        TSimpleThreadPool.shutdown();
    }

    public void startByDir(String rootSrc, String rootDest, JSONObject jo) throws Exception {
        // 先处理当前目录
        for (Entry entry : jo.entrySet()) {
            String gid = (String) entry.getKey();
            String gname = (String) entry.getValue();
            allInOne(gid, gname, rootSrc, rootDest);
        }

        // 在递归
        List<String> sonDirList = TFileUtils.getDirList(rootSrc);
        for (String sonPath : sonDirList) {
            String name = FilenameUtils.getBaseName(sonPath);
            String destPath = new File(rootDest, name).getAbsolutePath();
            startByDir(sonPath, destPath, jo);
        }
    }

    private void allInOne(String gid, String gname, String rootSrc, String rootDest) {
        GroupBean gb = new GroupBean(gid, gname, rootSrc, rootDest);
        //gb.genAll();
       gb.genImg();
    }

    private void mergePdf(String gid, String gname, String rootSrc, String rootDest) {
        GroupBean gb = new PdfGroupBean(gid, gname, rootSrc, rootDest);
        gb.genPdf();
    }
}
